<?php
declare (strict_types=1);

namespace app\adminapi\http\middleware;

use app\common\{
    cache\AdminAuthCache,
    service\JsonService
};
use think\helper\Str;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;

/**
 * 权限验证中间件
 * Class AuthMiddleware
 * @package app\adminapi\http\middleware
 */
class AuthMiddleware implements MiddlewareInterface
{

    public function process(Request $request, callable $next): Response
    {        //不登录访问，无需权限验证
        if ($request->controllerObject->isNotNeedLogin()) {
            return $next($request);
        }

        if (1 === $request->adminInfo['root']) {
            return $next($request);
        }

//        //系统默认超级管理员，无需权限验证
        $adminAuthCache = new AdminAuthCache($request->adminInfo['admin_id']);

        // 当前访问路径
//        $accessUri = strtolower($request->controller . '/' . $request->action);
        $controllerName = str_replace("app\\adminapi\\controller\\", "", $request->controller);
        $controllerName = str_replace("Controller", "", $controllerName);
        $accessUri = strtolower(str_replace("\\", "/", $controllerName). '/' . $request->action);

        // 全部路由
        $allUri = $this->formatUrl($adminAuthCache->getAllUri());

        // 判断该当前访问的uri是否存在，不存在无需验证
        if (!in_array($accessUri, $allUri)) {
            return $next($request);
        }

        // 当前管理员拥有的路由权限
        $AdminUris = $adminAuthCache->getAdminUri() ?? [];
        $AdminUris = $this->formatUrl($AdminUris);

        if (in_array($accessUri, $AdminUris)) {
            return $next($request);
        }
        return JsonService::fail('权限不足，无法访问或操作');

    }

    /**
     * @notes 格式化URL
     * @param array $data
     * @return array|string[]
     */
    public function formatUrl(array $data)
    {
        return array_map(function ($item) {
            return strtolower(Str::camel($item));
        }, $data);
    }

}